package com.ruoyi.common.wx.resolver;

import com.ruoyi.common.wx.annotation.AppUserId;
import com.ruoyi.common.wx.interceptor.AppAuthorizationInterceptor;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

@Component
public class AppLoginUserIdHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter parameter)
    {
        return parameter.getParameterType().isAssignableFrom(Long.class) && parameter.hasParameterAnnotation(AppUserId.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container,
                                  NativeWebRequest request, WebDataBinderFactory factory)
    {
        //获取用户ID
        return Long.valueOf(String.valueOf(request.getAttribute(AppAuthorizationInterceptor.USER_KEY, RequestAttributes.SCOPE_REQUEST)));
    }
}
